package io.github.kimmking;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

public class Main {


    public static void main(String[] args) {
        HashMap<String,String> map = new HashMap<>();
        map.put("baseproject","D:\\env\\pm\\baseproject");
        map.put("management","D:\\kk\\ctssvn\\management");
        map.put("project","D:\\kk\\ctssvn\\project");
        map.put("kuaipan","D:\\kuaipan");

        for (Map.Entry<String,String> entry : map.entrySet()){
            File file = new File(entry.getValue());
            if(file.exists())
                processFile(entry.getKey(),file);
        }
    }

    private static void processFile(String rootName,File file){
        if(file.getName().startsWith(".")) return;
        if(file.isDirectory())
            recordDir(rootName,file);
        else
            recordFile(rootName,file);
    }

    private static void recordDir(String rootName,File dir){
        recordLog(rootName, dir.getName(),0,0,dir.lastModified(),"",dir.getAbsolutePath());
        File[] fs = dir.listFiles();
        for(File f : fs)
            processFile(rootName,f);
    }

    private static void recordFile(String rootName,File file){
        recordLog(rootName, file.getName(),1,file.length(),file.lastModified(),ext(file.getName()),file.getAbsolutePath());
    }

    private static void recordLog(String rootName,String name,int type, long length, long last,String ext,String path){
        log("insert sfile(category,fname,type,length,lastmodified,extname,path) values('" + rootName + "','" + name
                + "'," + type + "," + length + "," + last + ",'" + ext + "','" + path.replace("\\", "\\\\") +"');");
    }

    private static void log(String str){
        System.out.println(str);
    }

    private static String ext(String fn){
        int pos = fn.indexOf(".");
        if(pos< 0)
            return "";
        else
            return fn.substring(pos + 1);
    }

}
